import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "@/views/Login.vue";
import {getToken} from "@/utils/auth";

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/',
    name: 'Dashboard',
    component: ()=>import('@/views/Home.vue'),
    children: [
      {
        path: 'system/user',
        name: 'User',
        component: ()=>import('@/views/system/user/index.vue')
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if(getToken() !== undefined && getToken() !== null && getToken() !== ''){
    if(to.name === 'Login') next({path: '/'})
    else next()
  }else if(to.name !== 'Login') next({path: '/login'}) //如果没有token，且不是登录页面，跳转到登录页面
  else next() //如果没有token，且是登录页面，放行
})

export default router
